<?php
/**
 * 银行卡设置
 * User: Administrator
 * Date: 2016/3/20
 * Time: 18:20
 */

namespace Admin\Controller;

class BankListController extends CommonController{

    protected function _initialize(){
        parent::_initialize();
        $this->breadcrumb1='系统';
        $this->breadcrumb2='银行设置';
    }

    public function index(){

        $this->list=M('BankList')->select();

        $this->display();
    }

    function add(){
        if(IS_POST){
            $bank_name = $_POST["bank_name"];//配置参数
            $status = $_POST["status"];//配置参数
            $data['bank_name'] = $bank_name;
            $data['status'] = $status;

            $error=$this->validate($bank_name);

            if(!empty($error)){
                $this->error($error["message"]);
            }

            $r=M('BankList')->add($data);
            if($r){
                $this->success('新增成功',U('BankList/index'));
            }else{
                $this->error('新增失败');
            }
        }

        $this->crumbs='新增';
        $this->action=U('BankList/add');
        $this->display('edit');
    }


    function edit(){

        $id=I('id');

        if(IS_POST){
            $data = array();
            $id = intval($_POST["id"]);
            $payment_config	= '';
            $bank_name = $_POST["bank_name"];//配置参数

            $error=$this->validate($bank_name);

            if(!empty($error)){
                $this->error($error["message"]);
            }

            $status = $_POST["status"];//配置参数
            $data['bank_name'] = $bank_name;
            $data['status'] = $status;
            $r=M('BankList')->where(array('bank_id'=>$id))->save($data);
            //echo M('BankList')->getLastSql();exit;
            if($r){
                $this->success('编辑成功',U('BankList/index'));
            }else{
                $this->error('编辑失败');
            }
            die;
        }
        $this->action=U('BankList/edit');

        $bank = M('BankList')->where(array('bank_id'=>$id))->find();

        $this->bank_array=array(
            'bank_id'=>$bank['bank_id'],
            'bank_name'=>$bank['bank_name'],
            'status'=>$bank['status']
        );
        $this->crumbs="编辑";

        $this->display();
    }

    private function validate($data){

        $error=array();
        if(empty($data)){
            $error='银行名称必填';
        }

        if($error){

            return array(
                'status'=>'back',
                'message'=>$error
            );

        }
        return null;
    }



}
?>